package com.umeox.watch.worshiplearn.ui

import android.os.Bundle
import android.util.LayoutDirection
import android.util.Log
import androidx.core.text.TextUtilsCompat
import androidx.viewpager2.widget.ViewPager2
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.ImageView
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.worshiplearn.R
import com.umeox.watch.worshiplearn.adapter.LmanViewPagerAdapter
import com.umeox.watch.worshiplearn.model.LmanItem
import java.util.Locale

/**
 * Created by Rambo 2022/9/10
 * Describe:
 */
class LmanViewPagerActivity : ThemeAppCompatActivity() {

    private lateinit var mViewPager: ViewPager2
    private lateinit var mIndexTv: TextView
    private lateinit var mBtnLast: ImageView
    private lateinit var mBtnNext: ImageView

    private lateinit var dataList: ArrayList<LmanItem>

    private var mCurrentItemIndex: Int = 0

    private lateinit var nameArr: Array<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.iman_viewpager_activity)
        nameArr = arrayOf(
            getString(R.string.str_allah),
            getString(R.string.str_his_angels),
            getString(R.string.str_his_books),
            getString(R.string.str_his_messengers),
            getString(R.string.str_day_of_judgment),
            getString(R.string.str_qadhaa)
        )
        initView()
        initData()
    }

    private fun initView() {
        mViewPager = findViewById(R.id.vp)
        mIndexTv = findViewById(R.id.tv_index)

        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
            Log.e("TAG", "LayoutDirection.RTL")
            mBtnLast = findViewById(R.id.iv_right)
            mBtnNext = findViewById(R.id.iv_left)
        } else {
            mBtnLast = findViewById(R.id.iv_left)
            mBtnNext = findViewById(R.id.iv_right)
        }
    }


    private fun initData() {
        val imagerRes = resources.getStringArray(R.array.iman_images)
        dataList = ArrayList()

        for ((index, item) in imagerRes.withIndex()) {
            val resId = resources.getIdentifier(item, "drawable", packageName)
            val name = nameArr[index]
            dataList.add(LmanItem(name, resId))
        }


        mViewPager.adapter = LmanViewPagerAdapter(dataList)
        mCurrentItemIndex = 0
        mViewPager.currentItem = mCurrentItemIndex
        mViewPager.registerOnPageChangeCallback(ViewPageChange())
        mBtnLast.setOnClickListener {
            mViewPager.currentItem = mCurrentItemIndex - 1
        }

        mBtnNext.setOnClickListener {
            mViewPager.currentItem = mCurrentItemIndex + 1
        }
    }

    inner class ViewPageChange : ViewPager2.OnPageChangeCallback() {

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            mCurrentItemIndex = position
            mIndexTv.text = String.format(Locale.getDefault(), "%d", position + 1)
            when (position) {
                0 -> {
                    mBtnLast.isEnabled = false
                    mBtnLast.alpha = 0f
                }

                (dataList.size - 1) -> {
                    mBtnNext.isEnabled = false
                    mBtnNext.alpha = 0f
                }

                else -> {
                    mBtnLast.isEnabled = true
                    mBtnNext.isEnabled = true
                    mBtnLast.alpha = 1f
                    mBtnNext.alpha = 1f
                }
            }
        }
    }

}
